<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
          <el-form-item label="业主编号" prop="ownerId">
          <el-input v-model="dataForm.ownerId" placeholder="业主编号"></el-input>
      </el-form-item>
          <el-form-item label="物业管理员编号" prop="managerId">
          
          <el-select v-model="dataForm.managerId" placeholder="物业管理员编号">
            <el-option
              v-for="manager in managerList"
              :key="manager.id"
              :label="manager.id+'号负责人  '+manager.name"
              :value="manager.id"
            >
            </el-option>
          </el-select>
      </el-form-item>
          <el-form-item label="报修时间" prop="repairTime">
          
          <el-date-picker
              v-model="dataForm.repairTime" placeholder="报修时间" type="date" value-format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
      </el-form-item>
          <el-form-item label="报修内容" prop="content">
          <el-input type="textarea" v-model="dataForm.content" placeholder="报修内容"></el-input>
      </el-form-item>
          <el-form-item label="保修状态" prop="state">
          <el-radio v-model="dataForm.state" placeholder="保修状态" label="0">未处理</el-radio>
          <el-radio v-model="dataForm.state" placeholder="保修状态" label="1">已处理</el-radio>
      </el-form-item>
          <el-form-item label="备注" prop="memo">
          <el-input type="textarea" v-model="dataForm.memo" placeholder="备注"></el-input>
      </el-form-item>
            
          <el-form-item label="更新人" prop="updator">
          <el-input v-model="dataForm.updator" placeholder="更新人"></el-input>
      </el-form-item>
          
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        ownerId: '',
        managerId: '',
        repairTime: '',
        content: '',
        state: '',
        memo: '',
        creator: '',
        createTime: '',
        updator: '',
        updateTime: ''
      },
      managerList:[]
    }
  },
  computed: {
    dataRule () {
      return {
        ownerId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        managerId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        repairTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        content: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        state: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        memo: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        createTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updator: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updateTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    getManagerList(){
      this.$http.get(`eightapp/eimanagerinfo/getmanagerlist`)
      .then(({data:res}) =>{
        this.managerList=res.data;
        console.log(this.managerList)
      })
    },
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/eightapp/eirepairinfo/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/eightapp/eirepairinfo/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  },
  created() {
    this.getManagerList();
  }
}
</script>
